/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef SPATIALAWARENESS_MGR_SERVICE_H
#define SPATIALAWARENESS_MGR_SERVICE_H

#include <mutex>
#include <memory>
#include <iremote_object.h>
#include "system_ability.h"
#include "delayed_sp_singleton.h"

#include "spatial_awareness_mgr_stub.h"
#include "spatial_awareness_epoch.h"
#include "spatial_coordinate_system.h"
#include "spatial_communicator.h"
#include "spatial_mgr_event_handler.h"
#include "spatial_mgr_service_inner.h"
#include "spatial_device_manager.h"
#include "spatial_algorithm_handle.h"

namespace OHOS {
namespace Msdp {
class SpatialaWarenessMgrService final : public SystemAbility, public SpatialaWarenessMgrStub {
    DECLARE_SYSTEM_ABILITY(SpatialaWarenessMgrService)
    DECLARE_DELAYED_SP_SINGLETON(SpatialaWarenessMgrService);

public:
    using AlgorithmInstMap = std::map<AlgorithmType, std::shared_ptr<MsdpAlgorithmInterface>>;
    virtual void OnStart() override;
    virtual void OnStop() override;
    virtual bool Init(bool mock);
    virtual int Dump(int fd, const std::vector<std::u16string> &args) override;
    virtual float GetDistance(std::string deviceId) override;
    virtual float GetAngle(std::string deviceId) override;
    virtual uint32_t GetDirection(std::string deviceId) override;
    virtual void SubscribeSpatialLocationRelationship(const SpatialLocationMode& mode,
        const SpatialDeviceInfo &info, const sptr<ISpatialLocationCallback>& callback) override;
    virtual void UnSubscribeSpatialLocationRelationship(const SpatialLocationMode& mode,
        const SpatialDeviceInfo &info, const sptr<ISpatialLocationCallback>& callback) override;
    virtual void StartMonitor();
    virtual void StopMonitor();
    virtual void HandleEvent(int32_t event);
    virtual void HandleLoadEvent(int32_t event);
    std::shared_ptr<SpatialMgrEventHandler> GetHandler()
    {
        return handler_;
    }

    std::shared_ptr<SpatialMgrServiceInner> GetSpatialInner()
    {
        return inner_;
    }
    std::shared_ptr<SpatialDeviceManager> GetDeviceManager()
    {
        return deviceMgr_;
    }
    std::shared_ptr<SpatialCoordinateSystem> GetCoordinateSystem()
    {
        return coordinateSystem_;
    }
    AlgorithmInstMap GetAlgorithmInstMap()
    {
        return algorithmMap_;
    }
private:
    static const int64_t DEVICE_INFO_UPDATE_TIME = 10000;
    bool InitEventHandler();
    bool InitCommunication();
    bool InitSpatialInner();
    void InitSpatialDeviceManager();
    void DiscoverDevice();
    void InitLibALgo();
    void SendEvent(int32_t event, int64_t param, int64_t delayTime = 0);
    void RemoveEvent(int32_t event);
    void UpdateDeviceInfo();
    void GetAllLibInst();
    void CheckDeviceManager();
    std::shared_ptr<AppExecFwk::EventRunner> eventRunner_;
    std::shared_ptr<SpatialMgrEventHandler> handler_;
    std::shared_ptr<SpatialMgrServiceInner> inner_;
    std::shared_ptr<SpatialDeviceManager> deviceMgr_;
    std::shared_ptr<SpatialCommunicator> communicator_;
    std::shared_ptr<SpatialCoordinateSystem> coordinateSystem_;
    std::shared_ptr<SpatialAwarenessEpoch> epoch_;
    std::shared_ptr<SpatialAlgorithmHandle> algorithmHandle_;
    bool monitoring_ {false};
    bool mock_ {false};
    AlgorithmInstMap algorithmMap_;
};
} // namespace Msdp
} // namespace OHOS

#endif // SPATIALAWARENESS_MGR_SERVICE_H
